package com.box.controller;

import java.io.PrintWriter;

import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.box.entity.DbAdmin;
import com.box.entity.DbTheme;
import com.box.service.AdminService;
import com.box.service.ThemeService;
import com.box.utils.LayuiResult;

/**
 * @author 轮播图管理
 *
 */
@Controller
@RequestMapping("/theme")
public class ThemeController {
	
	@Autowired
	private AdminService adminService;
	
	@Autowired
	private ThemeService themeService;
	
	//获取列表
	@RequestMapping(value="/getThemeList",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public LayuiResult lookThemeList(Integer page,Integer limit,HttpServletResponse response) throws Exception{

		 response.setHeader("Access-Control-Allow-Origin", "*");
		
		
	
		return 	themeService.selThemeList(page, limit);
	}
	
	//单个删除
	@RequestMapping(value="/delThemeByid/{id}",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public LayuiResult delThemeByid(@PathVariable("id") String id){
		Integer n = themeService.delThemeByid(id);
		if(n>0){
			return new LayuiResult(0,"删除成功");
		}else{
			return new LayuiResult(500, "删除失败");
		}
	

	}
	
	
	//修改
	@RequestMapping(value="/updTheme",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public LayuiResult updTheme(DbTheme theme){
						Integer n = themeService.updateTheme(theme);
						if(n>0){
							return new LayuiResult(0, "修改成功");
						}else{
							return new LayuiResult(500, "修改失败");
						}
	
		
	}
	
	
	//添加
	@RequestMapping(value="/insertTheme",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public LayuiResult insertTheme(DbTheme theme){
						Integer n = themeService.insertTheme(theme);
						if(n>0){
							return new LayuiResult(0, "添加成功");
						}else{
							return new LayuiResult(500, "添加失败");
						}
	
		
	}
	
	
	//批量删除
	@RequestMapping(value="/delThemeAll/{ThemeStr}",method={RequestMethod.POST,RequestMethod.GET})
	@ResponseBody
	public LayuiResult delThemeAll(@PathVariable("ThemeStr") String ThemeStr){
						Integer n = themeService.deleteByAll(ThemeStr);
						if(n>0){
							return new LayuiResult(0, "批量删除成功");
						}else{
							return new LayuiResult(500, "批量删除失败");
						}
	
		
	}
	
	
	/**
	 * 
	 * 
	 * 
	 * ===============页面跳转controller
	 * @param model
	 * @return
	 */
	@RequestMapping(value="/page/ThemeList",method={RequestMethod.POST,RequestMethod.GET})
	public String showThemeList(Model model){
						
		DbAdmin admin = adminService.getDbAdminItems();
		model.addAttribute("admin", admin);
		return "theme/ThemeList" ;
	}
	
	@RequestMapping(value="/page/EditTheme/{id}",method={RequestMethod.POST,RequestMethod.GET})
	public String showEditPage(@PathVariable("id") String id,Model model){
						
		DbAdmin admin = adminService.getDbAdminItems();
		model.addAttribute("admin", admin);
		
		DbTheme theme = themeService.selThemeByOne(id);
		model.addAttribute("theme", theme);
		return "theme/EditTheme" ;
	}
	//添加
	@RequestMapping(value="/page/AddTheme",method={RequestMethod.POST,RequestMethod.GET})
	public String showAddPage(Model model){
						
		DbAdmin admin = adminService.getDbAdminItems();
		model.addAttribute("admin", admin);
		
		
		return "theme/AddTheme" ;
	}

}
